package com.wt.admin.domain.vo;


import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

@Data
public class ProgressVO {

    @JsonIgnore
    public static ThreadLocal<ProgressVO> map = new ThreadLocal<>();

    private Long id;

    private String title;

    private Integer total = 100;

    private Integer current = 0;

    private String status = null;

    private String callback = "";

    public static ProgressVO set(String title,String callback,Integer current){
        ProgressVO progressVO = new ProgressVO();
        progressVO.setCallback(callback);
        if(map.get() != null){
            progressVO = map.get();
            progressVO.setCurrent(current);
            if(current >= progressVO.getTotal()){
                progressVO.setStatus("success");
                remove();
            }
            if(current < 0){
                progressVO.setStatus("exception");
                remove();
            }
            return progressVO;
        }
        progressVO.setTitle(title);
        progressVO.setCurrent(current);
        progressVO.setId(System.currentTimeMillis());
        if(current >= progressVO.getTotal()){
            progressVO.setStatus("success");
            remove();
        }
        if(current < 0){
            progressVO.setStatus("exception");
            remove();
        }
        map.set(progressVO);
        return progressVO;
    }


    public static ProgressVO set(String title,Integer current){
        return set(title, null, current);
    }

    public static ProgressVO set(Integer current){
        ProgressVO progressVO = map.get();
        progressVO.setCurrent(current);
        if(current >= progressVO.getTotal()){
            progressVO.setStatus("success");
            remove();
        }
        if(current < 0){
            progressVO.setStatus("exception");
            remove();
        }
        return progressVO;
    }

    public static void remove(){
        map.remove();
    }


}
